/**
 * @file setting_sd_auto_mount.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SD_AUTO_MOUNT != 0

/*********************
 *      DEFINES
 *********************/
setting_sd_auto_mount_t settingmount;
lv_obj_t * enquire_tip_cont = NULL;
lv_obj_t * success_tip_cont = NULL;
lv_obj_t * remove_tip_cont = NULL;
TASK_HANDLE * mount_sd_task = NULL;
lv_task_t * capture_sd_task = NULL;
lv_task_t * auto_mount_success_task = NULL;
lv_task_t * remove_capture_task = NULL;
lv_task_t * del_sd_mount_task = NULL;
bool release_capture_app_proc = false;

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * sd_auto_mount_create(lv_obj_t * activity_obj);
static void setting_set_sd_auto_mount(bool sd_auto_mount);
static bool setting_get_sd_auto_mount(void);
static void sd_auto_mount_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void sd_auto_mount_ok_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void sd_mounted_as_storage_enquire_tip(void);
static void sd_mounted_as_storage_event_cb(lv_obj_t * btn, lv_event_t event);
static void sd_mounted_as_storage_success_tip(void);
static void sd_mounted_usb_remove_event_cb(lv_obj_t * btn, lv_event_t event);
static void sd_mounted_usb_remove_tip(void);
static void sd_capture_by_app_task(void);
static void sd_mount_by_usb_task(void);
static void auto_mount_success_task_create(void);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/
#define SETTING_SD_MOUNTED_AS_U_STORAGE           (SD_MOUNTED_AS_U_STORAGE)
#define SETTING_SD_REQUEST_UNMOUNT_FROM_U_STORAGE (SD_REQUEST_UNMOUNT_FROM_U_STORAGE)
#define SETTING_SD_REMOUNT_OK                     (SD_REMOUNT_OK)

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void sd_auto_mount_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = sd_auto_mount_create(NULL);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) return;
}

void sd_auto_mount_init(nv_watch_settings_t * nvsettings)
{
    memset(&settingmount, 0, sizeof(setting_sd_auto_mount_t));

    if(0 == nvsettings->sdautomount) {
        settingmount.sd_auto_mount = false;
    } else {
        settingmount.sd_auto_mount = true;
#if USE_LV_SD_FS
        while(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
            HAL_sd_user_set(false);
        }
#endif
    }
}

bool setting_sd_mount_capture_check(uint8_t app_type)
{
    bool status = false;
#if USE_LV_SD_FS
    if(SETTING_SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_set(true)) {
        printf("%s: SD card is mounted as U storage!\n",__FUNCTION__);
        status = true;
    } else {
        settingmount.capture_sd_app |= app_type;
    }
#endif
    return status;
}

void setting_sd_mount_app_release(uint8_t app_type)
{
    if(app_type & settingmount.capture_sd_app) {
        settingmount.capture_sd_app &= ~(app_type);
#if USE_LV_SD_FS
        if(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
            HAL_sd_user_set(false);
        }
#endif
        printf("%s,SD card is released by user!\n",__FUNCTION__);
    }
    if(0 == settingmount.capture_sd_app) {
        if(true == setting_get_sd_auto_mount()) {
#if USE_LV_SD_FS
            while(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
                HAL_sd_user_set(false);
            }
            if((HAL_USB_CONNECTED == Hal_Get_Usb_Status()) && (SETTING_SD_REMOUNT_OK == HAL_sd_user_get())){
                printf("%s: SD card mounted as U storage autotically!\n",__FUNCTION__);
#if USE_LV_WATCH_AUDIO_PLAYER != 0
                audio_player_for_sd_mount();
#endif
                sd_mount_by_usb_task();
            }
#endif
        } else {
#if USE_LV_SD_FS
            if(SETTING_SD_REMOUNT_OK == HAL_sd_user_get()) {
                HAL_sd_user_set(true);
            }
#endif
        }
    }
}

void setting_sd_mount_common_destory_capture_app(void)
{
    if(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER & settingmount.capture_sd_app) {
        printf("%s: release audio_player app\n", __FUNCTION__);
        setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER);
#if USE_LV_WATCH_AUDIO_PLAYER != 0
        audio_player_for_mount_destory();
#endif
        if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER));
        }
        if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS));
        }
        if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT));
        }
        if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS));
        }
    }
    if(SETTING_CAPTURE_SD_APP_IMGVIEWER & settingmount.capture_sd_app) {
        printf("%s: release imgviewer app\n", __FUNCTION__);
        setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_IMGVIEWER);
#if USE_LV_WATCH_IMGVIEWER != 0
        imgviewer_for_mount_destory();
#endif
        if(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER));
        }
        if(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER_SETTING)) {
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER_SETTING));
        }
    }

    while(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
        HAL_sd_user_set(false);
    }
    sd_mount_by_usb_task();
}

void setting_sd_mount_capture_by_usb(void)
{
#if USE_LV_SD_FS
    if(true == Hal_Get_Sd_Ready()) {
        if(true == setting_get_sd_auto_mount()) {
            if(0 == settingmount.capture_sd_app) {
                printf("%s: SD card mounted as U storage!\n",__FUNCTION__);
#if USE_LV_WATCH_AUDIO_PLAYER != 0
                audio_player_for_sd_mount();
#endif
                auto_mount_success_task_create();
            } else {
                printf("%s: SD card has been captured by APP!\n",__FUNCTION__);
            }
        } else {
            sd_mounted_as_storage_enquire_tip();
        }
    } else {
        printf("%s: no sdcard.\n", __FUNCTION__);
    }
#endif
}

void setting_sd_mount_remove(void)
{
    if(true == Hal_Get_Sd_Ready()) {
        sd_mounted_usb_remove_tip();
        if(false == setting_get_sd_auto_mount()) {
            sd_capture_by_app_task();
        }
#if USE_LV_SD_FS && USE_LV_WATCH_AUDIO_PLAYER != 0
        audio_player_for_sd_unmount();
#endif
    }  else {
        printf("%s: no sdcard.\n", __FUNCTION__);
    }
}

void setting_sd_mount_on_clear_ui(void)
{
    if(true == Hal_Get_Sd_Ready()) {
        sd_mounted_usb_remove_event_cb(NULL, LV_EVENT_CLICKED);
    }
}

void setting_sd_mount_off_clear_ui(void)
{
    if(true == Hal_Get_Sd_Ready()) {
        sd_auto_mount_cancel_btn_event_cb(NULL, LV_EVENT_CLICKED);
        sd_mounted_as_storage_event_cb(NULL, LV_EVENT_CLICKED);
    }
}

void setting_set_release_capture_app_proc(bool status)
{
    release_capture_app_proc = status;
    printf("%s: release_capture_app_proc = %d\n", __FUNCTION__, release_capture_app_proc);
}

bool setting_get_release_capture_app_proc(void)
{
    printf("%s: release_capture_app_proc = %d\n", __FUNCTION__, release_capture_app_proc);
    return release_capture_app_proc;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void sd_auto_mount_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void setting_set_sd_auto_mount(bool sd_auto_mount)
{
    uint8_t nvdata = 0;

    // update global variable
    settingmount.sd_auto_mount = sd_auto_mount;

    // nv write
    if(true == sd_auto_mount) {
        nvdata = 1;
    }
    printf("%s write sd_auto_mount to nvm, %d\n", __FUNCTION__, nvdata);
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 7, 1, &nvdata);
}

static bool setting_get_sd_auto_mount(void)
{
    printf("%s: sd_auto_mount = %d\n", __FUNCTION__, settingmount.sd_auto_mount);
    return settingmount.sd_auto_mount;
}

static lv_sd_auto_mount_obj_ext_t * sd_auto_mount_get_ext(void)
{
    lv_sd_auto_mount_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SD_AUTO_MOUNT);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}

static void sd_mounted_usb_remove_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    if(remove_tip_cont) {
        lv_obj_del(remove_tip_cont);
        remove_tip_cont = NULL;
    }
}

static void remove_tip_cont_cleanup_cb(lv_obj_t * obj, lv_event_t event)
{
    if(LV_EVENT_DELETE == event)
    {
        remove_tip_cont = NULL;
    }
}

static void sd_mounted_usb_remove_tip(void)
{
    lv_obj_t * par;
    lv_obj_t * phone_obj = lv_watch_get_activity_obj(ACT_ID_PHONE);
    lv_obj_t * voipphone_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);

    if(phone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, phone_obj);
    else if(voipphone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, voipphone_obj);
    else
        par = lv_watch_get_top_activity_obj();

    lv_obj_t * cont_display = lv_cont_create(par, NULL);
    lv_obj_set_event_cb(cont_display,remove_tip_cont_cleanup_cb);
    lv_obj_set_size(cont_display, LV_HOR_RES, LV_VER_RES);
    lv_obj_align(cont_display, par, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont_display, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_obj_set_click(cont_display, true);

    lv_obj_t * cont = lv_cont_create(cont_display, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 50, LV_VER_RES - 50);
    lv_obj_align(cont, cont_display, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_pretty);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_USB_REMOVE);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, LV_HOR_RES - 80);
    lv_obj_align(label, cont, LV_ALIGN_CENTER, 0, -50);

    lv_obj_t * btn_ok = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn_ok, LV_HOR_RES * 2 / 3, 30);
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_t * label_ok = lv_label_create(btn_ok, NULL);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
    lv_obj_add_style(label_ok, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_align(btn_ok, cont, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
    lv_obj_add_protect(btn_ok, LV_PROTECT_PRESS_LOST);
    lv_obj_set_event_cb(btn_ok, sd_mounted_usb_remove_event_cb);

    remove_tip_cont = cont_display;
}

static void sd_mounted_as_storage_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    if(success_tip_cont) {
        lv_obj_del(success_tip_cont);
        success_tip_cont = NULL;
    }
}

static void success_tip_cont_cleanup_cb(lv_obj_t * obj, lv_event_t event)
{
    if(LV_EVENT_DELETE == event)
    {
        success_tip_cont = NULL;
    }
}

static void sd_mounted_as_storage_success_tip(void)
{
    lv_obj_t * par;
    lv_obj_t * phone_obj = lv_watch_get_activity_obj(ACT_ID_PHONE);
    lv_obj_t * voipphone_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);

    if(phone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, phone_obj);
    else if(voipphone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, voipphone_obj);
    else
        par = lv_watch_get_top_activity_obj();

    lv_obj_t * cont_display = lv_cont_create(par, NULL);
    lv_obj_set_event_cb(cont_display,success_tip_cont_cleanup_cb);
    lv_obj_set_size(cont_display, LV_HOR_RES, LV_VER_RES);
    lv_obj_align(cont_display, par, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont_display, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_obj_set_click(cont_display, true);

    lv_obj_t * cont = lv_cont_create(cont_display, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 50, LV_VER_RES - 50);
    lv_obj_align(cont, cont_display, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_pretty);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_MOUNT_AS_U_STORAGE);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, LV_HOR_RES - 80);
    lv_obj_align(label, cont, LV_ALIGN_CENTER, 0, -50);

    lv_obj_t * btn_ok = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn_ok, LV_HOR_RES * 2 / 3, 30);
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_t * label_ok = lv_label_create(btn_ok, NULL);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
    lv_obj_add_style(label_ok, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_align(btn_ok, cont, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
    lv_obj_add_protect(btn_ok, LV_PROTECT_PRESS_LOST);
    lv_obj_set_event_cb(btn_ok, sd_mounted_as_storage_event_cb);

    success_tip_cont = cont_display;
}

static void sd_auto_mount_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    if(enquire_tip_cont) {
        lv_obj_del(enquire_tip_cont);
        enquire_tip_cont = NULL;
    }
}

static void sd_auto_mount_ok_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    if(enquire_tip_cont) {
        lv_obj_del(enquire_tip_cont);
        enquire_tip_cont = NULL;
    }
    if(settingmount.capture_sd_app) {
        if(SETTING_CAPTURE_SD_APP_BLUETOOTH & settingmount.capture_sd_app) {
#if USE_LV_BLUETOOTH != 0
            printf("%s: release bluetooth app\n", __FUNCTION__);
            setting_set_release_capture_app_proc(true);
            setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_BLUETOOTH);
            bluetooth_for_mount_destory();
#endif
        } else {
            setting_sd_mount_common_destory_capture_app();
        }
    } else {
        while(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
            HAL_sd_user_set(false);
        }
        sd_mount_by_usb_task();
    }
#if USE_LV_SD_FS && USE_LV_WATCH_AUDIO_PLAYER != 0
    audio_player_for_sd_mount();
#endif
    printf("%s: SETTING_SD_MOUNTED_AS_U_STORAGE\n", __FUNCTION__);
}

static void enquire_tip_cont_cleanup_cb(lv_obj_t * obj, lv_event_t event)
{
    if(LV_EVENT_DELETE == event)
    {
        enquire_tip_cont = NULL;
    }
}

static void sd_mounted_as_storage_enquire_tip(void)
{
    lv_obj_t * par;
    lv_obj_t * phone_obj = lv_watch_get_activity_obj(ACT_ID_PHONE);
    lv_obj_t * voipphone_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);

    if(phone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, phone_obj);
    else if(voipphone_obj)
        par = _lv_ll_get_next(&(lv_scr_act())->child_ll, voipphone_obj);
    else
        par = lv_watch_get_top_activity_obj();

    lv_obj_t * cont_display = lv_cont_create(par, NULL);
    lv_obj_set_event_cb(cont_display,enquire_tip_cont_cleanup_cb);
    lv_obj_set_size(cont_display, LV_HOR_RES, LV_VER_RES);
    lv_obj_align(cont_display, par, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont_display, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_obj_set_click(cont_display, true);

    lv_obj_t * cont = lv_cont_create(cont_display, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 50, LV_VER_RES - 50);
    lv_obj_align(cont, cont_display, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_pretty);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_MOUNT_AS_U_STORAGE_Q);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, LV_HOR_RES - 80);
    lv_obj_align(label, cont, LV_ALIGN_CENTER, 0, -50);

    lv_obj_t * btn_cancel = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn_cancel, LV_HOR_RES / 3, 30);
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_t * label_cancel = lv_label_create(btn_cancel, NULL);
    lv_label_set_text_id(label_cancel, WATCH_TEXT_ID_CANCEL);
    lv_obj_add_style(label_cancel, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_add_protect(btn_cancel, LV_PROTECT_PRESS_LOST);
    lv_obj_set_event_cb(btn_cancel, sd_auto_mount_cancel_btn_event_cb);

    lv_obj_t * btn_ok = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn_ok, LV_HOR_RES / 3, 30);
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_t * label_ok = lv_label_create(btn_ok, NULL);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
    lv_obj_add_style(label_ok, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_add_protect(btn_ok, LV_PROTECT_PRESS_LOST);
    lv_obj_set_event_cb(btn_ok, sd_auto_mount_ok_btn_event_cb);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont)) {
        lv_obj_align(btn_ok, cont, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
        lv_obj_align(btn_cancel, cont, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    } else {
        lv_obj_align(btn_cancel, cont, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
        lv_obj_align(btn_ok, cont, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    }

    enquire_tip_cont = cont_display;
}

static void sd_auto_mount_on_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_sd_auto_mount_obj_ext_t * ext = sd_auto_mount_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_on, true);
    if(false == setting_get_sd_auto_mount()) {
        lv_checkbox_set_checked(ext->cb_off, false);
        setting_set_sd_auto_mount(true);
#if USE_LV_SD_FS
        if(0 == settingmount.capture_sd_app) {
            while(SETTING_SD_REMOUNT_OK < HAL_sd_user_get()) {
                HAL_sd_user_set(false);
            }
        }
#endif
    }
}

static void sd_auto_mount_off_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_sd_auto_mount_obj_ext_t * ext = sd_auto_mount_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_off, true);
    if(true == setting_get_sd_auto_mount()) {
        lv_checkbox_set_checked(ext->cb_on, false);
        setting_set_sd_auto_mount(false);
#if USE_LV_SD_FS
        if(SETTING_SD_REMOUNT_OK <= HAL_sd_user_get()) {
            HAL_sd_user_set(true);
        }
#endif
    }
}

static lv_obj_t * sd_auto_mount_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SD_AUTO_MOUNT;
        activity_ext.create = sd_auto_mount_create;
        activity_ext.prepare_destory = sd_auto_mount_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_sd_auto_mount_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_sd_auto_mount_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_AUTO_MOUNT);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cb_on = lv_checkbox_create(obj, NULL);
    lv_checkbox_set_text(cb_on, lv_lang_get_text(WATCH_TEXT_ID_ON));
    lv_obj_clean_style_list(cb_on, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(cb_on, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(cb_on, sd_auto_mount_on_btn_event_cb);
    lv_watch_obj_add_element(cb_on);

    lv_obj_t * cb_off = lv_checkbox_create(obj, cb_on);
    lv_checkbox_set_text(cb_off, lv_lang_get_text(WATCH_TEXT_ID_OFF));
    lv_obj_set_event_cb(cb_off, sd_auto_mount_off_btn_event_cb);
    lv_watch_obj_add_element(cb_off);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(obj)) {
        lv_obj_align(cb_on, obj, LV_ALIGN_CENTER, 10, -30);
        lv_obj_align(cb_off, cb_on, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);
    } else {
        lv_obj_align(cb_on, obj, LV_ALIGN_CENTER, -10, -30);
        lv_obj_align(cb_off, cb_on, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    }

    if(setting_get_sd_auto_mount()) {
        lv_checkbox_set_checked(cb_on, true);
        lv_checkbox_set_checked(cb_off, false);
    } else {
        lv_checkbox_set_checked(cb_on, false);
        lv_checkbox_set_checked(cb_off, true);
    }

    ext->cb_on = cb_on;
    ext->cb_off = cb_off;
    return obj;
}

static void sd_capture_by_app_handle(lv_task_t * task)
{
#if USE_LV_SD_FS
    if(SETTING_SD_REMOUNT_OK == HAL_sd_user_set(true)) {
        lv_task_del(capture_sd_task);
        capture_sd_task = NULL;
    } else {
        printf("%s: SD mount by USB\n", __FUNCTION__);
    }
#endif
}

static void sd_capture_by_app_task(void)
{
    if(NULL == capture_sd_task) {
        capture_sd_task = lv_task_create(sd_capture_by_app_handle, 10, LV_TASK_PRIO_HIGH, NULL);
    }
}

static void delete_sd_mount_task_handle(lv_task_t * task)
{
    if(mount_sd_task != NULL) {
        printf("%s: delete sd mount task\n", __FUNCTION__);
        uos_delete_task(mount_sd_task);
        mount_sd_task = NULL;
    }
    lv_task_del(del_sd_mount_task);
    del_sd_mount_task = NULL;
}

static void delete_sd_mount_task(void)
{
    if(NULL == del_sd_mount_task) {
        del_sd_mount_task = lv_task_create(delete_sd_mount_task_handle, 10, LV_TASK_PRIO_HIGH, NULL);
    }
}

static void sd_mount_by_usb_handle(void * para)
{
    printf("%s\n", __FUNCTION__);
    if(HAL_USB_DISCONNECTED == Hal_Get_Usb_Status()) {
        printf("%s: usb status is disconnect.\n", __FUNCTION__);
    } else {
#if USE_LV_SD_FS
        if(SETTING_SD_REMOUNT_OK == HAL_sd_user_get()) {
            HAL_sd_user_set(SETTING_SD_MOUNTED_AS_U_STORAGE);
            printf("%s: SETTING_SD_MOUNTED_AS_U_STORAGE\n", __FUNCTION__);
            if(false == Hal_Get_Sd_Ready()) {
                printf("%s: no sdcard.\n", __FUNCTION__);
                goto exit;
            }
            if(false == Hal_Get_Sd_Fat_Ok()) {
                printf("%s: SD card FAT32 system is not ok\n", __FUNCTION__);
                goto exit;
            }
            if(false == Hal_Connect_To_Pc()) {
                printf("%s: usb is not connect to PC.\n", __FUNCTION__);
#if USE_LV_WATCH_AUDIO_PLAYER != 0
                audio_player_for_sd_unmount();
#endif
                goto exit;
            }
            //enable U storage;
            USBDeviceSelect(USB_STORAGE_ONLY);
            auto_mount_success_task_create();
            delete_sd_mount_task();
            return;
        }
#endif
    }
exit:
    HAL_sd_user_set(SETTING_SD_REMOUNT_OK);
    delete_sd_mount_task();
}

static void sd_mount_by_usb_task(void)
{
    if(NULL == mount_sd_task) {
        printf("%s: SETTING_SD_MOUNTED_AS_U_STORAGE\n", __FUNCTION__);
        mount_sd_task = uos_create_task(sd_mount_by_usb_handle, NULL, 0, 0x2000, 240, "mount_sd_task");
    }
}

static void auto_mount_success_handle(lv_task_t * task)
{
#if USE_LV_SD_FS
    if(SETTING_SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_get()) {
        sd_mounted_as_storage_success_tip();
        lv_task_del(auto_mount_success_task);
        auto_mount_success_task = NULL;
    }
#endif
}

static void auto_mount_success_task_create(void)
{
    if(NULL == auto_mount_success_task) {
        auto_mount_success_task = lv_task_create(auto_mount_success_handle, 10, LV_TASK_PRIO_HIGH, NULL);
    }
}

#endif /* USE_LV_WATCH_SD_AUTO_MOUNT */
